<template>
	<view class="content">
		<view class="flex-r jsb">
			<view
				class="title fb textCenter"
				:class="{ on: index == isActive }"
				v-for="(item, index) in tab"
				@click="handletabClick(index)"
			>
				{{ item }}
			</view>
		</view>
		<template v-if="isActive == 0">
			<view class="mt16 fm pb10">
				<input type="text" placeholder="输入手机号" v-model="form.userName" />
				<input type="password" placeholder="输入登录密码" v-model="form.userPassword" />
				<view class="remember-password flex-r">
					<checkbox :checked="isRememberUser" @click="toggleRememberUser" />
					记住密码
				</view>
			</view>
			<view class="btn white textCenter mt16" @tap="submit(1)">登录</view>
			<view class="btn white textCenter mt16" @tap="wxLogin">微信一键登录</view>
		</template>

		<template v-else>
			<view class="mt16 fm pb10">
				<input type="text" placeholder="输入手机号" v-model="adminForm.userName" />
				<input type="password" placeholder="输入登录密码" v-model="adminForm.userPassword" />
				<view class="remember-password flex-r">
					<checkbox :checked="isRememberAdmin" @click="toggleRememberAdmin" />
					记住密码
				</view>
			</view>
			<view class="btn white textCenter mt16" @tap="submit(2)">登录</view>
		</template>

		<view class="textCenter mt16">
			<text style="color: #999">没有账号？</text>
			<text style="color: #c40414" @tap="handleClick">立即注册</text>
		</view>
	</view>
</template>

<script>
import Utils from '@/utils/utils.js'
import { login, register, wxOpenidLogin, getWxOpenid } from '@/api/login.js'
export default {
	data() {
		return {
			isActive: 0,
			tab: ['用户登录', '商户登录'],
			form: {
				userName: '',
				userPassword: ''
			},
			adminForm: {
				userName: '',
				userPassword: ''
			},
			isRememberUser: false, // 是否记住用户密码
			isRememberAdmin: false // 是否记住商户密码
		}
	},
	onLoad(option) {
		this.isActive = option.type || 0
		// 从本地存储中读取记住的用户名和密码
		this.loadRememberedCredentials()
	},
	methods: {
		handletabClick(index) {
			this.isActive = index
		},
		// 切换用户记住密码
		toggleRememberUser() {
			this.isRememberUser = !this.isRememberUser
		},

		// 切换商户记住密码
		toggleRememberAdmin() {
			this.isRememberAdmin = !this.isRememberAdmin
		},
		// 从本地存储中加载记住的用户名和密码
		loadRememberedCredentials() {
			const rememberedUser = uni.getStorageSync('rememberedUser')
			if (rememberedUser) {
				this.form.userName = rememberedUser.userName
				this.form.userPassword = rememberedUser.password
				this.isRememberUser = true
			}
			console.log('USER--->', rememberedUser)

			const rememberedAdmin = uni.getStorageSync('rememberedAdmin')
			if (rememberedAdmin) {
				this.adminForm.userName = rememberedAdmin.userName
				this.adminForm.userPassword = rememberedAdmin.password
				this.isRememberAdmin = true
				console.log('-->', this.isRememberAdmin)
			}
			console.log('ADMIN-->', rememberedAdmin)
		},
		submit(state) {
			switch (state) {
				case 1:
					console.log('游客登录')
					let params = {
						username: `hy_${this.form.userName}`,
						password: this.form.userPassword
					}
					login(params).then(
						(res) => {
							console.log(res)
							if (res.code == 200) {
								if (res.token) {
									Utils.saveToken(res.token)
								}
								uni.setStorageSync('loginState', 1)
								// 如果用户选择了记住密码，则保存到本地存储
								if (this.isRememberUser) {
									uni.setStorageSync('rememberedUser', {
										userName: this.form.userName,
										password: this.form.userPassword
									})
								} else {
									// 如果取消记住密码，则清除本地存储
									uni.removeStorageSync('rememberedUser')
								}
								uni.switchTab({
									url: '/pages/mine/mine',
									success: function () {
										setTimeout(() => {
											console.log('登录成功!')
										}, 300)
									}
								})
							} else {
								Utils.toast(res.msg)
							}
						},
						(err) => {
							console.log(err)
						}
					)
					break
				case 2:
					console.log('商户登录')
					let adminParams = {
						username: `sh_${this.adminForm.userName}`,
						password: this.adminForm.userPassword
					}
					login(adminParams).then(
						(res) => {
							console.log(res)
							if (res.code == 200) {
								if (res.token) {
									Utils.saveToken(res.token)
								}
								uni.setStorageSync('loginState', 2)
								// 如果商户选择了记住密码，则保存到本地存储
								if (this.isRememberAdmin) {
									uni.setStorageSync('rememberedAdmin', {
										userName: this.adminForm.userName,
										password: this.adminForm.userPassword
									})
									console.log('记住商户密码', uni.getStorageSync('rememberedAdmin'))
								} else {
									// 如果取消记住密码，则清除本地存储
									uni.removeStorageSync('rememberedAdmin')
								}
								uni.switchTab({
									url: '/pages/mine/mine',
									success: function () {
										setTimeout(() => {
											console.log('登录成功!')
										}, 300)
									}
								})
							} else {
								Utils.toast(res.msg)
							}
						},
						(err) => {
							console.log(err)
						}
					)
					break
			}
		},
		wxLogin() {
			let openid = uni.getStorageSync('openid')
			if (openid) {
				wxOpenidLogin(openid).then(
					(res) => {
						console.log(res)
						if (res.token == undefined) {
							Utils.toast('请先注册!')
						} else {
							Utils.saveToken(res.token)
							uni.setStorageSync('loginState', 1)
							uni.switchTab({
								url: '/pages/mine/mine',
								success: function () {
									// setTimeout(() => {
									// 	Utils.toast('登录成功!')
									// }, 300)
								}
							})
						}
					},
					(err) => {
						console.log(err)
					}
				)
			} else {
				uni.login({
					provider: 'weixin',
					success: async (res) => {
						console.log('res-login', res)
						if (res.errMsg == 'login:ok') {
							getWxOpenid(res.code).then(
								(res) => {
									if (res.code == 200) {
										//uni.setStorageSync('openid', res.result.openid)
										let openId = res.result.openid
										console.log(openId)
										wxOpenidLogin(openId).then(
											(res) => {
												console.log('==>', res, '>>', res.token)
												if (res.code == 200) {
												} else {
													Utils.toast(res.msg)
												}
												if (res.token == undefined) {
													uni.reLaunch({
														url: '/pages_B/register/register',
														success: function () {
															setTimeout(() => {
																Utils.toast('请先注册!')
															}, 300)
														}
													})
												} else {
													Utils.saveToken(res.token)
													uni.setStorageSync('loginState', 1)
													uni.switchTab({
														url: '/pages/mine/mine',
														success: function () {
															// setTimeout(() => {
															// 	Utils.toast('登录成功!')
															// }, 300)
														}
													})
												}
											},
											(err) => {
												console.log(err)
											}
										)
									}
								},
								(err) => {
									console.log(err)
								}
							)
						}
					}
				})
			}
		},
		handleClick() {
			uni.navigateTo({
				url: `/pages_B/register/register?type=${this.isActive}`
			})
		}
	}
}
</script>

<style lang="stylus" scoped>
.content
	min-height 100%
	padding 80rpx
.title
	width 150rpx
	line-height 80rpx
.on
	border-bottom 4rpx solid #c40414
.fm input
	border none
	height 120rpx
	line-height 120rpx
	border-bottom 1rpx solid #ccc
.btn
	line-height 100rpx
	height 100rpx
	border-radius 50rpx
	background #c40414
.remember-password
	margin-top 20rpx
	color #999
	checkbox
		margin-right 10rpx
</style>